﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class LocationAdvertisement : System.Web.UI.Page
    {
        private static LocationAdvertisementBO boqc;
        private static LocationAdvertisementDetailBO qcdetailbo;
        private static OptionBO optionbo;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckBox checkboxqc = new CheckBox();
            int vitriqcid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
            boqc = LocationAdvertisementBO.GetLocationAdvertisementBO(vitriqcid);
            txtvitriid.Text = boqc.LocationAdversitementID.ToString();
            txtvitriid.Enabled = false;
            txttenvitri.Text = boqc.LocationAdversitementName;
            checkboxqc = (CheckBox)GridView1.SelectedRow.FindControl("cbIsActive");
            cbkichhoat.Checked = checkboxqc.Checked;
            if (boqc.LocationAdvertisementDetailBOList.Count > 0)
            {
                GridView2.DataSource = boqc.LocationAdvertisementDetailBOList;
                GridView2.DataBind();
                GridView2.Visible = true;
            }
            else
                GridView2.Visible = false;
            if (pn_danhsach_qc_chitiet.Visible == false) pn_danhsach_qc_chitiet.Visible = true;
            lbmessage.Visible = false;
            Pn_location_detail.Visible = false;
        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lbchitietvitrichitiet.Text = "Sửa vị trí quảng cáo chi tiết";
                messagedetail.Text = "";
                Pn_location_detail.Visible = true;
                btnAddvitrichitietDetail.Visible = false;
                btnUpdatevitrichitietDetail.Visible = true;
                txtvitrichitiet.Text = GridView2.SelectedRow.Cells[2].Text;
                var tindangkyqc = (LinkButton)GridView2.SelectedRow.FindControl("LbtTitle");
                if (tindangkyqc.Text.Trim() != "")
                {
                    txttindangkyquangcao.Text = tindangkyqc.Text;
                }
                else
                {
                    txttindangkyquangcao.Text = "Vị Trí Chưa Có Tin Đăng Ký Quảng Cáo";
                }
                string option = "";
                int idqc;
                idqc = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
                if (idqc == 1)
                {
                    option = "SoViTriQCTren";
                }
                else if (idqc == 2)
                {
                    option = "SoViTriQCTrai";
                }
                else if (idqc == 3)
                {
                    option = "SoViTriQCPhai";
                }
                dlvitrihienthi.Items.Clear();
                optionbo = OptionBO.GetNameOptionBO(option);
                for (int i = 1; i <= Convert.ToInt32(optionbo.OptionValue); i++)
                    dlvitrihienthi.Items.Add(i.ToString());
                dlvitrihienthi.SelectedValue = GridView2.SelectedRow.Cells[4].Text;
                CheckBox cb = (CheckBox)GridView2.SelectedRow.FindControl("CheckBox1");
                isActivevitrichitiet.Checked = cb.Checked;
                txtvitrichitiet.Visible = true;
                lbvitrichitietID.Visible = true;
            }
            catch
            { }
        }

        protected void btnhuy_Click(object sender, EventArgs e)
        {
            pn_danhsach_qc_chitiet.Visible = false;
            Pn_location_detail.Visible = false;
        }

        protected void btncapnhat_Click(object sender, EventArgs e)
        {
            int vitriid1 = Convert.ToInt32(txtvitriid.Text);
            //boqc = LocationAdvertisementBO.GetLocationAdvertisementBO(vitriid1);
            boqc.LocationAdversitementName = txttenvitri.Text;
            boqc.IsActive = cbkichhoat.Checked;
            try
            {
                boqc.Save();
                GridView1.DataBind();
                lbmessage.Text = "Cập nhật thành công";               
                GridView1_SelectedIndexChanged(null, null);
            }
            catch
            {
                lbmessage.Text = "Cập nhật thất bại";
            }
            lbmessage.Visible = true;
        }

        protected void btnUpdatevitrichitietDetail_Click(object sender, EventArgs e)
        {
            try
            {
                //boqc = LocationAdvertisementBO.GetLocationAdvertisementBO(Convert.ToInt32(txtvitriid.Text));
                qcdetailbo = boqc.LocationAdvertisementDetailBOList.Single(b => b.LocationAdversitementDetailID == Convert.ToInt32(txtvitrichitiet.Text));                
                LocationAdvertisementDetailBO qcdetailbo_kiemtra;
                try
                {
                    qcdetailbo_kiemtra = boqc.LocationAdvertisementDetailBOList.Single(a => a.LocationInWeb == dlvitrihienthi.SelectedValue);
                }
                catch
                {
                    qcdetailbo_kiemtra = null;
                }
                if (qcdetailbo_kiemtra == null)
                {
                    qcdetailbo.LocationInWeb = dlvitrihienthi.SelectedValue;
                    qcdetailbo.IsActive = isActivevitrichitiet.Checked;
                    messagedetail.Text = "Cập nhật thành công";
                    boqc.Save();
                    messagedetail.Visible = true;
                    GridView2.DataSource = boqc.LocationAdvertisementDetailBOList;
                    GridView2.DataBind();
                    return;
                }
                else if (qcdetailbo_kiemtra.LocationAdversitementDetailID == Convert.ToInt32(txtvitrichitiet.Text))
                {
                    qcdetailbo.LocationInWeb = dlvitrihienthi.SelectedValue;
                    qcdetailbo.IsActive = isActivevitrichitiet.Checked;
                    messagedetail.Text = "Cập nhật thành công";
                    boqc.Save();
                    messagedetail.Visible = true;
                    GridView2.DataSource = boqc.LocationAdvertisementDetailBOList;
                    GridView2.DataBind();                    
                }
                else
                {
                    messagedetail.Text = "Vị trí quảng cáo này đã có";
                    messagedetail.Visible = true;
                    return;
                }
            }
            catch
            {
                messagedetail.Text = "Cập nhật thất bại";
                messagedetail.Visible = true;
            }            
        }

        protected void btndong_Click(object sender, EventArgs e)
        {
            Pn_location_detail.Visible = false;
        }

        protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                GridViewRow selectrow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
                //boqc = LocationAdvertisementBO.GetLocationAdvertisementBO(Convert.ToInt32(txtvitriid.Text));
                qcdetailbo = boqc.LocationAdvertisementDetailBOList.Single(a => a.LocationAdversitementDetailID == Convert.ToInt32(selectrow.Cells[2].Text));
                boqc.LocationAdvertisementDetailBOList.Remove(qcdetailbo);
                //GridView2.DeleteRow(selectrow.RowIndex);
                GridView2.DataSource = boqc.LocationAdvertisementDetailBOList;
                GridView2.DataBind();
                messagedelete.Text = "Đã xóa";
                Pn_location_detail.Visible = false;
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            messagedetail.Text = "";
            lbchitietvitrichitiet.Text = "Thêm vị trí quảng cáo chi tiết";
            txtvitrichitiet.Visible = false;
            lbvitrichitietID.Visible = false;
            txttindangkyquangcao.Text = "Do Người Dùng Đăng Ký";
            string option = "";
            int idqc;
            idqc = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
            if (idqc == 1)
            {
                option = "SoViTriQCTren";
            }
            else if (idqc == 2)
            {
                option = "SoViTriQCTrai";
            }
            else if (idqc == 3)
            {
                option = "SoViTriQCPhai";
            }
            dlvitrihienthi.Items.Clear();
            optionbo = OptionBO.GetNameOptionBO(option);
            for (int i = 1; i <= Convert.ToInt32(optionbo.OptionValue); i++)
                dlvitrihienthi.Items.Add(i.ToString());
            dlvitrihienthi.SelectedIndex = 0;
            btnAddvitrichitietDetail.Visible = true;
            btnUpdatevitrichitietDetail.Visible = false;
            Pn_location_detail.Visible = true;
        }

        protected void btnAddvitrichitietDetail_Click(object sender, EventArgs e)
        {
            LocationAdvertisementDetailBO qcdetailbo_kiemtra;
            try
            {
                qcdetailbo_kiemtra = boqc.LocationAdvertisementDetailBOList.Single(a => a.LocationInWeb == dlvitrihienthi.SelectedValue);
            }
            catch
            {
                qcdetailbo_kiemtra = null;
            }
            if (qcdetailbo_kiemtra != null)
            {
                messagedetail.Text = "Vị trí quảng cáo này đã có";
            }
            else
            {
                LocationAdvertisementDetailBO detaibo_addnew;
                detaibo_addnew = boqc.LocationAdvertisementDetailBOList.AddNew();
                detaibo_addnew.LocationInWeb = dlvitrihienthi.SelectedValue;
                detaibo_addnew.IsActive = isActivevitrichitiet.Checked;
                GridView2.DataSource = boqc.LocationAdvertisementDetailBOList;
                GridView2.DataBind();
                messagedetail.Text = "Thêm thành công";
            }
        }
    }
}
